<?php
namespace  App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\ParkFloor;
use App\Models\ParkSpace;
use App\Models\ParkSpaces;
use App\Models\ParkUnit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ParkSpaceController extends Controller
{
    /*
     * 车位管理列表
     * 车位状态图
     */
    public function index(Request $request,ParkSpaces $parkSpaces,ParkFloor $parkFloor){
        $this->validate($request,[
            'community_id' => 'required|integer'
        ],[
            'community_id.required' => 'id不能为空',
            'community_id.integer' => 'id必须是数字'
        ]);
//        ini_set("error_reporting",E_ALL ^ E_NOTICE);
        //$parkSpaces->where('community_id','=',$request->community_id)->where('park_floor','=',$request->park_floor)->orderBy('park_sort')->get();
        $data = array();
//        $res = $parkSpaces->where('community_id','=',$request->community_id)->where('park_floor','=',$request->park_floor)->orderBy('park_sort')
//            ->get(['id','community_id','park_status','park_number']);
        //-------------
//        $res = $parkSpaces->where('community_id','=',$request->community_id)->orderBy('park_floor_number')->orderBy('park_sort')->get();
        $res = $parkSpaces->where('community_id','=',$request->community_id)->orderBy('park_floor_number')->orderBy('park_sort')->get();
        $floor_res = $parkFloor->where('community_id','=',$request->community_id)->get('park_floor');
        foreach ($floor_res as $k=>$v){
            $data[]['park_floor'] = $v['park_floor'];
        }
        foreach ($res as $k=>$v) {
            switch($v['park_status']){
                case 1:
                    $res[$k]['park_status_d'] = '闲置';
                    break;
                case 2:
                    $res[$k]['park_status_d'] = '出租';
                    break;
                case 3:
                    $res[$k]['park_status_d'] = '自用';
                    break;
                case 4:
                    $res[$k]['park_status_d'] = '人防';
                    break;
            }
        }
        foreach ($data as $k=>$v){
            for($i = 0;$i < count($res); $i++){
                if($v['park_floor'] == $res[$i]['park_floor']){
                    $data[$k]['park'][] = $res[$i]->toArray();
                }
            }
        }
        if($data){
            return $this->return_json(200,$data,'查询成功');
        }else{
            return $this->return_json(201,'','数据为空');
        }
    }

    /*
     * 根据小区id、车位楼层获取数据
     */
    public function indexParkFloor(Request $request,ParkSpaces $parkSpaces)
    {
        $this->validate($request,[
            'community_id' => 'required'
        ],[
            'community_id.required' => 'community_id不能为空'
        ]);
        $res = $parkSpaces->where('community_id','=',$request->community_id)->where('park_floor','=',$request->park_floor)->orderBy('park_sort')->get();
        foreach ($res as $k=>$v) {
            switch($v['park_status']){
                case 1:
                    $res[$k]['park_status_d'] = '闲置';
                    break;
                case 2:
                    $res[$k]['park_status_d'] = '出租';
                    break;
                case 3:
                    $res[$k]['park_status_d'] = '自用';
                    break;
                case 4:
                    $res[$k]['park_status_d'] = '人防';
                    break;
            }
        }
        if($res){
            return $this->return_json(200,$res,'查询成功');
        }else{
            return $this->return_json(201,'','数据为空');
        }
    }

    public function save(){


    }

    /*
     * 根据停车位id获取车位详情
     */
    public function parkOne(Request $request,ParkSpaces $parkSpaces)
    {
        $this->validate($request,[
            'id' => 'required'
        ],[
            'id.required' => 'id不能为空'
        ]);
        $res = $parkSpaces->where('id','=',$request->id)->get();
        if($res){
            return $this->return_json(200,$res,'查询成功');
        }else{
            return $this->return_json(201,'','数据为空');
        }
    }
    /*
    * 保存停车位详情信息
    */
    public function update(Request $request,ParkSpaces $parkSpaces){
        $code = $parkSpaces->where('id','=',$request->id)->update($request->all());
        if($code){
            return $this->return_json(200,'','添加成功');
        }else{
            return $this->return_json(201,'','添加失败');
        }
    }

    public function delete(){

    }

    /*
     * 添加停车位
     */
    public function addParkSpace(Request $request,ParkSpaces $parkSpaces)
    {
        $param = $request->all();
        //$park_status = $request->park_status;//车位状态
        //$parks = $request->parks;//楼层车位数量
        $park_floor = $request->park_floor;//所在楼层
        //$community_id = $request->community_id;//所属小区id
        $park_floor_number = 0;
        switch ($park_floor){
            case '地面':
                $park_floor_number = 1;
                $this->addPark($parkSpaces,$param,$park_floor_number);
                break;
            case '负一楼':
                $park_floor_number = 2;
                $this->addPark($parkSpaces,$param,$park_floor_number);
                break;
            case '负二楼':
                $park_floor_number = 3;
                $this->addPark($parkSpaces,$param,$park_floor_number);
                break;
            case '负三楼':
                $park_floor_number = 4;
                $this->addPark($parkSpaces,$param,$park_floor_number);
                break;
            default:
                return $this->return_json(201, '', '创建失败');
        }

        return $this->return_json(200, '', '创建成功');
    }

    public function addPark($parkSpaces,$param,$park_floor_number)
    {
        $parks = $param['parks'];//楼层车位数量
        $park_floor = $param['park_floor'];//所在楼层
        $community_id = $param['community_id'];//所属小区id
        $param['park_floor_number'] = $park_floor_number;
        $num = $parkSpaces->where('community_id','=',$community_id)->where('park_floor','=',$park_floor)->count();
        if($num > 0){
            for($i = $num+1 ;$i <= $parks+$num ;$i++){
                $param['park_number'] = $community_id.'-'.$park_floor_number.'-'.$i;
                $param['park_sort'] = $i;
                DB::table('park_spaces')->insert($param);//执行循环添加
            }
        }else{
            for($i = 1 ;$i <= $parks ;$i++){
                $param['park_number'] = $community_id.'-'.$park_floor_number.'-'.$i;
                $param['park_sort'] = $i;
                DB::table('park_spaces')->insert($param);//执行循环添加
            }
        }
    }

    /*
    * 创建栋数 单元 层数 车位编号  创建车位表
    * */
    public function addListStoried(Request $request, ParkFloor $parkFloor, ParkUnit $parkUnit)
    {
//      $request->community_id;//小区ID 绑定小区id
//      $request->floor;//楼号 栋数
//      $request->unit;//单元
//      $request->layer_number;//层数
//      $request->parks;//车位数
        $parkFloor->community_id = $request->community_id;
        $parkFloor->number = $request->floor;
        $data = ['community_id' => $parkFloor->community_id, 'number' => $request->floor,
            'comment' => $request->floor . '栋', 'parks' => $request->parks, 'unit' => $request->unit,
            'layer_number' => $request->layer_number
        ];
//        dd($data);die;
        $fllor_res = $parkFloor->create($data);//添加栋数
//        dd($fllor_res->toArray());die;
        $res = ['floor_id' => $fllor_res->id, 'number' => $request->unit, 'comment' => $request->unit . '单元'];
        $unit_res = $parkUnit->create($res);//添加单元 绑定栋数
        $data = [];
        $data['park_unit_id'] = $unit_res->id;//所属单元id
        for ($i = 1; $i <= $request->layer_number; $i++) { //先循环层数
            $a = $i;
            for ($x = 1; $x <= $request->parks; $x++) { //在循环车位数
//              dump($i . '楼' . $i . '0' . $x);
                $data['park_number'] = $request->floor . '-' . $request->unit . '-' . $x;//车位编号
                $data['park_floor'] = $i;//所在楼层
                $data['park_floor_number'] = $request->floor;//所在楼号
                $data['park_unit_number'] = $request->unit;//所属单元号
                $data['park_status'] = 1;//车位状态默认为 闲置
                DB::table('park_space')->insert($data);//执行循环添加
            }
        }
        return $this->return_json(200, '', '创建成功');
    }

    /*
    * 车位表记录数
    * */
    public function parkListNum(Request $request,ParkSpaces $parkSpaces)
    {
        if(isset($request->condition) && !empty($request->condition)){
            $condition = $request->condition;
            $query = 'SELECT COUNT(*) as count FROM `park_spaces` WHERE CONCAT(`park_floor`,`park_number`,`park_status`,`parks`) LIKE ?';
            $param = '%'.$condition.'%';
            $count = DB::select($query,array($param));
            $res = $count[0]->count;
        }else{
            $res = $parkSpaces->count();
        }
        if($res){
            return $this->return_json(200,$res,'查询成功');
        }else{
            return $this->return_json(201,'','查询失败');
        }
    }

    /*
    * 车位表列表
    * */
    public function parkList(Request $request,ParkSpaces $parkSpaces)
    {
        //5	1	1	1	1	1	1	1			1	2	1-1-4	4	地面	100		2019-07-26 17:17:18	1	1
        //5	1	1	1	1	1	1	1			1	2	1-1-81	81	地面	100		2019-08-06 09:30:05	1	1
        $page = $request->page;
        $limit = $request->limit;
        $start = ($page-1)*$limit;
        if(isset($request->condition) && !empty($request->condition)){
            $condition = $request->condition;
            $query = 'SELECT `id`,`park_floor`,`parks`,`park_status`,`park_sort`,`park_number` FROM `park_spaces` WHERE CONCAT(`park_floor`,`park_number`,`park_status`,`parks`) LIKE ? ORDER By park_floor_number limit ?,?';
            $param = '%'.$condition.'%';
            $res = DB::select($query , array($param,$start,$limit));
            json_decode(json_encode($res),true);
        }else{
            $res = $parkSpaces->orderBy('park_floor_number')->orderBy('park_sort')->offset($start)->limit($limit)->get(['id','park_floor','parks','park_status','park_sort','park_number']);
        }
        if($res && count($res) >0){
            return $this->return_json(200,$res,'查询成功');
        }else{
            return $this->return_json(201,'','查询数据为空');
        }
    }

    /*
     * 删除车位表
     * */
    public function deletePark(Request $request,ParkSpaces $parkSpaces)
    {
        $this->validate($request,[
            'id' => 'required|integer'
        ],[
            'id.required' => 'id不能为空',
            'id.integer' => 'id必须是数字'
        ]);
        $code = $parkSpaces->whereid($request->id)->delete();
       if($code){
           return $this->return_json(200,'','删除成功');
       }else{
           return $this->return_json(201,'','删除失败');
       }

    }

    /*
     * 渲染车位修改
     */
    public function parkRender(Request $request,ParkSpaces $parkSpaces)
    {
        $this->validate($request,[
            'id' => 'required|integer'
        ],[
            'id.required' => 'id不能为空',
            'id.integer' => 'id必须是数字'
        ]);
        $res = $parkSpaces->whereid($request->id)->get(['id','park_floor','parks','park_status','park_sort','park_number','community_id']);
        if($res && !$res->isEmpty()){
            return $this->return_json(200,$res,'查询成功');
        }else{
            return $this->return_json(201,'','数据为空');
        }
    }

    /*
     * 车位修改
     */
    public function parkUpdate(Request $request,ParkSpaces $parkSpaces)
    {
        $this->validate($request,[
            'id' => 'required|integer'
        ],[
            'id.required' => 'id不能为空',
            'id.integer' => 'id必须是数字'
        ]);
        $param = $request->all();


        $park_res = $parkSpaces->where('id','=',$param['id'])->get('community_id');
        $community_id = $park_res->toArray()[0]['community_id'];




        if($request->park_floor){
            $park_floor = $request->park_floor;
            switch ($request->park_floor){
                case '地面':
                    $park_floor_number = 1;
                    $code = $this->parkByCommunityIdAndFloor($parkSpaces,$community_id,$park_floor,$park_floor_number,$param);
                    break;
                case '负一楼':
                    $park_floor_number = 2;
                    $code = $this->parkByCommunityIdAndFloor($parkSpaces,$community_id,$park_floor,$park_floor_number,$param);
                    break;
                case '负二楼':
                    $park_floor_number = 3;
                    $code = $this->parkByCommunityIdAndFloor($parkSpaces,$community_id,$park_floor,$park_floor_number,$param);
                    break;
                case '负三楼':
                    $park_floor_number = 4;
                    $code = $this->parkByCommunityIdAndFloor($parkSpaces,$community_id,$park_floor,$park_floor_number,$param);
                    break;
                default:
                    return $this->return_json(201, '', '修改失败');
            }


            die();
            if($code == 200){
                return $this->return_json(200, '', '修改成功');
            }else{
                return $this->return_json(201, '', '修改失败');
            }
        }else{
            die();
            $code = $parkSpaces->where('id','=',$param['id'])->update($param);
            if($code){
                return $this->return_json(200, '', '修改成功');
            }else{
                return $this->return_json(201, '', '修改失败');
            }
        }
    }

    public function parkByCommunityIdAndFloor($parkSpaces,$community_id,$park_floor,$park_floor_number,$param)
    {
        $res = $parkSpaces->where('community_id','=',$community_id)->where('park_floor','=',$park_floor)->orderBy('park_sort','desc')->first();


            dd(json_encode($res));
        if($res){
            $res->toArray();
            $park_number = $res['park_number'];
            $park_sort = $res['park_sort'];
            $parks = explode('-',$park_number);
            $park_number = $parks[0].'-'.$parks[1].'-'.($park_sort+1);
            $park_sort+=1;
            $param['park_number'] = $park_number;
            $param['park_sort'] = $park_sort;
            $param['park_floor_number'] = $park_floor_number;
        }else{
            $park_number = $community_id.'-'.$park_floor_number.'-'.'1';
            $param['park_number'] = $park_number;
            $param['park_sort'] = 1;
            $param['park_floor_number'] = $park_floor_number;
        }

        dump($param);


//        $code = $parkSpaces->where('id','=',$param['id'])->update($param);
//        if($code){
//            return 200;
//        }else{
//            return 400;
//        }
    }
    /*
     * 获取小区所有楼号单元
     */
    public function parkSpaceFloorList(Request $request,ParkFloor $parkFloor,ParkUnit $parkUnit,ParkSpace $parkSpace)
    {
        $this->validate($request,[
            'id' => 'required|integer'
        ],[
            'id.required' => 'id不能为空',
            'id.integer' => 'id必须是数字'
        ]);
        $res = $parkFloor->where('community_id','=',$request->id)->orderBy('number')->orderBy('unit')->get();
        foreach($res as $k=>$v){
            $unit_res = $parkUnit->where('floor_id','=',$v['id'])->get();
//            dd($unit_res->toArray());die;
            $unit_res->toArray();
            if($unit_res && !$unit_res->isEmpty()){
//                dd($unit_res->id);die;
                $space_res = $parkSpace->where('park_unit_id','=',$unit_res[0]['id'])->get(['id','park_number','park_floor_number','park_unit_number','park_floor','park_status']);
                $res[$k]['desc'] = $v['number'].'号楼'.$v['unit'].'单元';
                $res[$k]['layer'] = $space_res;
//                $res[$k]['layer'] = $unit_res['id'];
//                $res[$k]['layer'] = $unit_res;
            }

        }
        if($res && !$res->isEmpty()){
            return $this->return_json(200,$res,'查询成功');
        }else{
            return $this->return_json(201,'','数据为空');
        }
    }

    /*
     * 添加小区停车位楼层
     */
    public function addCommunityParkFloor(Request $request,ParkFloor $parkFloor)
    {
        $code = $parkFloor->create($request->all());
        if($code){
            return $this->return_json(200,'','添加成功');
        }else{
            return $this->return_json(201,'','添加失败');
        }
    }

    /*
     * 获取小区停车位楼层
     */
    public function getCommunityParkFloor(Request $request,ParkFloor $parkFloor)
    {
        $res = $parkFloor->where('community_id','=',$request->community_id)->get();
        if($res){
            return $this->return_json(200,$res,'查询成功');
        }else{
            return $this->return_json(201,'','数据为空');
        }
    }
}
